package com.my.controller;


import com.alibaba.fastjson.JSON;
import com.my.bean.*;
import com.my.bean.pojoVo.MoenyInfoPojo;
import com.my.bean.pojoVo.MoneyMoth;
import com.my.bean.pojoVo.moneyPojo;
import com.my.service.MoenyDetailedService;
import com.my.service.OrderVipService;
import com.my.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 收费明细表   每个订单对应多个和明细表 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2018-08-25
 */
@Controller
@RequestMapping("/moenyDetailed")
public class MoenyDetailedController {
   @Autowired
   MoenyDetailedService moenyDetailedService;
   @Autowired
   OrderVipService orderVipService;
   @RequestMapping("/main")
   public String insertCharge(){
      return "moneryAdmin";
   }
   //交易明细的详情
   @RequestMapping("/MoenyInfo")
   public String MoenyInfo(@RequestParam("homeId") Long homeId , Model model){
      model.addAttribute("homeId",homeId);
      return "/Alert/moenyInfo";
   }

   //交易明细月报 跳转
   @RequestMapping("/moth.html")
   public String moth(){
      return "moeny_moth";
   }
   //交易明细月报 跳转
   @RequestMapping("/mothInfo.html")
   public String mothInfo(){
      return "/Alert/moeny_moth_info";
   }


   //给某个订单 添加新的消费记录
   @RequestMapping("/insertCharge")
   @ResponseBody
   public String insertCharge(MoenyDetailed moenyDetailed,Long homeId,HttpSession session){
      Long areaid =((User)session.getAttribute("user")).getAreaId();
      //房态管理用到   给某个房间的订单 添加新的额外消费
      if(homeId!=null&&homeId!=0){
         Map map=new HashMap<>();
         map.put("home_id",homeId);
         map.put("whether_home",2);
         map.put("areaid",areaid);
         List<OrderVip>    orderVips= orderVipService.selectByMap(map);
         moenyDetailed.setOrderId(orderVips.get(0).getId());
         moenyDetailed.setCreateTime(new Date().getTime());
      }

      Map<String, Object> jsonMap = new HashMap<String, Object>();
      moenyDetailed.setAreaid(areaid);
      if(moenyDetailedService.insert(moenyDetailed)) {
         //返回出提示消息
         jsonMap.put("flag",true);
         jsonMap.put("msg", "消费明细添加成功");
      }else{
         //返回出提示消息
         jsonMap.put("flag",false);
         jsonMap.put("msg", "失败");
      }
      return JSON.toJSONString(jsonMap);
   }



   //查询消费明细表格
   @RequestMapping("/list")
   @ResponseBody
   public ResultPage<moneyPojo> list(VoPage voPage,HttpSession session){
      boolean admin=false;
      //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
      if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
         admin=true;
      }
      Long areaid = ((User)session.getAttribute("user")).getAreaId();
      ResultPage<moneyPojo> resultPage= moenyDetailedService.selectMyPage(voPage,areaid,admin);
      return resultPage;
   }

   //查询消费明细表格

   @RequestMapping("/MoenyInfoData")
   @ResponseBody
   public List<MoenyInfoPojo> MoenyInfoData(Long homeId,HttpSession session){
      boolean admin=false;
      //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
      if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
         admin=true;
      }
      return  moenyDetailedService.findMoenySumCountByHomeId(homeId,admin);
   }


   //查询消费明细月报表格

   @RequestMapping("/mothData")
   @ResponseBody
   public ResultPage<MoneyMoth> mothData( VoPage voPage ,HttpSession session ){
      boolean admin=false;
      //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
      if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
         admin=true;
      }
      return  moenyDetailedService.findMothData(voPage  );
   }


}
